clear 
clear matrix
set memory 1000m
set more off
cap log close

cd "/Users/..."

global do_file="‎⁨/Users/.../do_file"
global log_file="/Users/.../log_file"
global raw_data="/Users/.../raw_data⁩⁩"
global working_data="/Users/.../working_data"
global results="/Users/.../results/072020"




*********************************
* For full sample, 15 NM measures 
*********************************



import delimited "$results/main_reg/reg_full_072020.txt", clear

 
 

drop v1 v2

drop if _n<=3 | _n==34 | _n>=36


forvalues i=3(1)182{

	loc j=`i'-2
	rename v`i' v`j' 
	
} 

forvalues i=1(1)30{
	
	loc j1=(`i'-1)*6+1
	loc j2=(`i'-1)*6+2
	loc j3=(`i'-1)*6+3
	loc j4=(`i'-1)*6+4
	loc j5=(`i'-1)*6+5
	loc j6=`i'*6
	
	preserve 
	
		keep v`j1'-v`j6' 
		drop if v`j1'==""
		
		rename v`j1' phone_use
		rename v`j2' distance
		rename v`j3' duration
		rename v`j4' speed
		rename v`j5' drive_at_night
		rename v`j6' highway
		
		save "$results/coef_temp_`i'.dta", replace
		
	restore
}

use "$results/coef_temp_1.dta", clear
drop if _n==_N

forvalues i=2(1)14{
	append using "$results/coef_temp_`i'.dta"
	drop if _n==_N
}

append using "$results/coef_temp_15.dta"

forvalues i=16(1)29{
	append using "$results/coef_temp_`i'.dta"
	drop if _n==_N
}

append using "$results/coef_temp_30.dta"


forvalues i=1(1)30{
	erase "$results/coef_temp_`i'.dta"  
}



** baseline 

preserve
keep if _n<=12 | _n==31


gen nm_measures="" 
replace nm_measures="No agg in the trip" if _n==1
replace nm_measures="No agg in the last 5 min" if _n==3
replace nm_measures="Original" if _n==5
replace nm_measures="No agg in the trip" if _n==7
replace nm_measures="No agg in the last 5 min" if _n==9
replace nm_measures="Original" if _n==11
order nm_measures, before(phone_use)

append using $results/main_reg/avg_values.dta


drop if _n>=14 & nm_measures~="full"
replace nm_measures="Average values" if _n==14
replace nm_measures="Observations" if _n==13

gen temp=_n
replace temp=13 if temp==14
sort temp nm_measures
drop temp

insobs 1, before(1)
replace nm_measures="" if _n==1
replace phone_use="Phone use" if _n==1
replace distance="Distance" if _n==1
replace duration="Duration" if _n==1
replace speed="Speed" if _n==1
replace drive_at_night="Drive at night" if _n==1
replace highway="Highway" if _n==1



texsave using "$results/tex_files/est_baseline_full.tex", replace  location(htbp) noendash nonames
!ex -sc '13i| \scalebox{0.8}{\begin{tabular}{lcccccc} \hline \hline' -cx "$results/tex_files/est_baseline_full.tex"
!ex -sc '15i| \hline ' -cx "$results/tex_files/est_baseline_full.tex"
!ex -sc '16i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_baseline_full.tex"
!ex -sc '17i| & \multicolumn{6}{c}{(a) Hard brakes}  \tabularnewline \cline{2-7} ' -cx "$results/tex_files/est_baseline_full.tex"
!ex -sc '24i| \hline ' -cx "$results/tex_files/est_baseline_full.tex"
!ex -sc '25i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_baseline_full.tex"
!ex -sc '26i| & \multicolumn{6}{c}{(b) Have both hard brakes and turns} \tabularnewline \cline{2-7} ' -cx "$results/tex_files/est_baseline_full.tex"
!ex -sc '33i| \hline ' -cx "$results/tex_files/est_baseline_full.tex"
!ex -sc '34i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_baseline_full.tex"
!ex -sc '37i| \hline \hline \end{tabular}} ' -cx "$results/tex_files/est_baseline_full.tex"

!ex -sc 38,42d -cx "$results/tex_files/est_baseline_full.tex"
!ex -sc 1,12d -cx "$results/tex_files/est_baseline_full.tex"

restore



** IV 

preserve
keep if (_n>=32 & _n<=43) | _n==62


gen nm_measures="" 
replace nm_measures="No agg in the trip" if _n==1
replace nm_measures="No agg in the last 5 min" if _n==3
replace nm_measures="Original" if _n==5
replace nm_measures="No agg in the trip" if _n==7
replace nm_measures="No agg in the last 5 min" if _n==9
replace nm_measures="Original" if _n==11
order nm_measures, before(phone_use)

append using $results/main_reg/avg_values.dta


drop if _n>=14 & nm_measures~="full"
replace nm_measures="Average values" if _n==14
replace nm_measures="Observations" if _n==13

gen temp=_n
replace temp=13 if temp==14
sort temp nm_measures
drop temp

insobs 1, before(1)
replace nm_measures="" if _n==1
replace phone_use="Phone use" if _n==1
replace distance="Distance" if _n==1
replace duration="Duration" if _n==1
replace speed="Speed" if _n==1
replace drive_at_night="Drive at night" if _n==1
replace highway="Highway" if _n==1



texsave using "$results/tex_files/est_iv_full.tex", replace  location(htbp) noendash nonames
!ex -sc '13i| \scalebox{0.8}{\begin{tabular}{lcccccc} \hline \hline' -cx "$results/tex_files/est_iv_full.tex"
!ex -sc '15i| \hline ' -cx "$results/tex_files/est_iv_full.tex"
!ex -sc '16i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_iv_full.tex"
!ex -sc '17i| & \multicolumn{6}{c}{(a) Hard brakes}  \tabularnewline \cline{2-7} ' -cx "$results/tex_files/est_iv_full.tex"
!ex -sc '24i| \hline ' -cx "$results/tex_files/est_iv_full.tex"
!ex -sc '25i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_iv_full.tex"
!ex -sc '26i| & \multicolumn{6}{c}{(b) Have both hard brakes and turns} \tabularnewline \cline{2-7} ' -cx "$results/tex_files/est_iv_full.tex"
!ex -sc '33i| \hline ' -cx "$results/tex_files/est_iv_full.tex"
!ex -sc '34i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_iv_full.tex"
!ex -sc '37i| \hline \hline \end{tabular}} ' -cx "$results/tex_files/est_iv_full.tex"

!ex -sc 38,42d -cx "$results/tex_files/est_iv_full.tex"
!ex -sc 1,12d -cx "$results/tex_files/est_iv_full.tex"

restore



**********************************
* Robustness checks, 6 NM measures 
**********************************




foreach name in  "regular" "later60" "early60" "exp" "inexp" "old" "male" ///
			     "rural" "urban" "dev" "ldev" {

import delimited "$results/main_reg/reg_`name'_072020.txt", clear



drop v1 v2

drop if _n<=3 | _n==16 | _n>=18


forvalues i=3(1)74{

	loc j=`i'-2
	rename v`i' v`j' 
	
} 

forvalues i=1(1)12{
	
	loc j1=(`i'-1)*6+1
	loc j2=(`i'-1)*6+2
	loc j3=(`i'-1)*6+3
	loc j4=(`i'-1)*6+4
	loc j5=(`i'-1)*6+5
	loc j6=`i'*6
	
	preserve 
	
		keep v`j1'-v`j6' 
		drop if v`j1'==""
		
		rename v`j1' phone_use
		rename v`j2' distance
		rename v`j3' duration
		rename v`j4' speed
		rename v`j5' drive_at_night
		rename v`j6' highway
		
		save "$results/coef_temp_`i'.dta", replace
		
	restore
}

use "$results/coef_temp_1.dta", clear
drop if _n==_N

forvalues i=2(1)5{
	append using "$results/coef_temp_`i'.dta"
	drop if _n==_N
}

append using "$results/coef_temp_6.dta"

forvalues i=7(1)11{
	append using "$results/coef_temp_`i'.dta"
	drop if _n==_N
}

append using "$results/coef_temp_12.dta"


forvalues i=1(1)12{
	erase "$results/coef_temp_`i'.dta"
}



** baseline 

preserve
keep if _n<=13 


gen nm_measures="" 
replace nm_measures="No agg in the trip" if _n==1
replace nm_measures="No agg in the last 5 min" if _n==3
replace nm_measures="Original" if _n==5
replace nm_measures="No agg in the trip" if _n==7
replace nm_measures="No agg in the last 5 min" if _n==9
replace nm_measures="Original" if _n==11
order nm_measures, before(phone_use)

append using $results/main_reg/avg_values.dta


drop if _n>=14 & nm_measures~="`name'"
replace nm_measures="Average values" if _n==14
replace nm_measures="Observations" if _n==13

gen temp=_n
replace temp=13 if temp==14
sort temp nm_measures
drop temp

insobs 1, before(1)
replace nm_measures="" if _n==1
replace phone_use="Phone use" if _n==1
replace distance="Distance" if _n==1
replace duration="Duration" if _n==1
replace speed="Speed" if _n==1
replace drive_at_night="Drive at night" if _n==1
replace highway="Highway" if _n==1



texsave using "$results/tex_files/est_baseline_`name'.tex", replace  location(htbp) noendash nonames
!ex -sc '13i| \scalebox{0.8}{\begin{tabular}{lcccccc} \hline \hline' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '15i| \hline ' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '16i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '17i| & \multicolumn{6}{c}{(a) Hard brakes}  \tabularnewline \cline{2-7} ' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '24i| \hline ' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '25i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '26i| & \multicolumn{6}{c}{(b) Have both hard brakes and turns} \tabularnewline \cline{2-7} ' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '33i| \hline ' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '34i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '37i| \hline \hline \end{tabular}} ' -cx "$results/tex_files/est_baseline_`name'.tex"

!ex -sc 38,42d -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc 1,12d -cx "$results/tex_files/est_baseline_`name'.tex"

restore



** IV 

preserve
keep if _n>=14


gen nm_measures="" 
replace nm_measures="No agg in the trip" if _n==1
replace nm_measures="No agg in the last 5 min" if _n==3
replace nm_measures="Original" if _n==5
replace nm_measures="No agg in the trip" if _n==7
replace nm_measures="No agg in the last 5 min" if _n==9
replace nm_measures="Original" if _n==11
order nm_measures, before(phone_use)

append using $results/main_reg/avg_values.dta


drop if _n>=14 & nm_measures~="`name'"
replace nm_measures="Average values" if _n==14
replace nm_measures="Observations" if _n==13

gen temp=_n
replace temp=13 if temp==14
sort temp nm_measures
drop temp

insobs 1, before(1)
replace nm_measures="" if _n==1
replace phone_use="Phone use" if _n==1
replace distance="Distance" if _n==1
replace duration="Duration" if _n==1
replace speed="Speed" if _n==1
replace drive_at_night="Drive at night" if _n==1
replace highway="Highway" if _n==1



texsave using "$results/tex_files/est_iv_`name'.tex", replace  location(htbp) noendash nonames
!ex -sc '13i| \scalebox{0.8}{\begin{tabular}{lcccccc} \hline \hline' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '15i| \hline ' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '16i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '17i| & \multicolumn{6}{c}{(a) Hard brakes}  \tabularnewline \cline{2-7} ' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '24i| \hline ' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '25i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '26i| & \multicolumn{6}{c}{(b) Have both hard brakes and turns} \tabularnewline \cline{2-7} ' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '33i| \hline ' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '34i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '37i| \hline \hline \end{tabular}} ' -cx "$results/tex_files/est_iv_`name'.tex"

!ex -sc 38,42d -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc 1,12d -cx "$results/tex_files/est_iv_`name'.tex"

restore

}





foreach name in  "familiarNM" "nonfamiliarNM" {

import delimited "$results/main_reg/reg_`name'_072020_new.txt", clear



drop v1 v2

drop if _n<=3 | _n==16 | _n>=18


forvalues i=3(1)74{

	loc j=`i'-2
	rename v`i' v`j' 
	
} 

forvalues i=1(1)12{
	
	loc j1=(`i'-1)*6+1
	loc j2=(`i'-1)*6+2
	loc j3=(`i'-1)*6+3
	loc j4=(`i'-1)*6+4
	loc j5=(`i'-1)*6+5
	loc j6=`i'*6
	
	preserve 
	
		keep v`j1'-v`j6' 
		drop if v`j1'==""
		
		rename v`j1' phone_use
		rename v`j2' distance
		rename v`j3' duration
		rename v`j4' speed
		rename v`j5' drive_at_night
		rename v`j6' highway
		
		save "$results/coef_temp_`i'.dta", replace
		
	restore
}

use "$results/coef_temp_1.dta", clear
drop if _n==_N

forvalues i=2(1)5{
	append using "$results/coef_temp_`i'.dta"
	drop if _n==_N
}

append using "$results/coef_temp_6.dta"

forvalues i=7(1)11{
	append using "$results/coef_temp_`i'.dta"
	drop if _n==_N
}

append using "$results/coef_temp_12.dta"


forvalues i=1(1)12{
	erase "$results/coef_temp_`i'.dta"
}



** baseline 

preserve
keep if _n<=13 


gen nm_measures="" 
replace nm_measures="No agg in the trip" if _n==1
replace nm_measures="No agg in the last 5 min" if _n==3
replace nm_measures="Original" if _n==5
replace nm_measures="No agg in the trip" if _n==7
replace nm_measures="No agg in the last 5 min" if _n==9
replace nm_measures="Original" if _n==11
order nm_measures, before(phone_use)

append using $results/main_reg/avg_values.dta


drop if _n>=14 & nm_measures~="full"   // use full sample avg
replace nm_measures="Average values" if _n==14
replace nm_measures="Observations" if _n==13

gen temp=_n
replace temp=13 if temp==14
sort temp nm_measures
drop temp

insobs 1, before(1)
replace nm_measures="" if _n==1
replace phone_use="Phone use" if _n==1
replace distance="Distance" if _n==1
replace duration="Duration" if _n==1
replace speed="Speed" if _n==1
replace drive_at_night="Drive at night" if _n==1
replace highway="Highway" if _n==1



texsave using "$results/tex_files/est_baseline_`name'.tex", replace  location(htbp) noendash nonames
!ex -sc '13i| \scalebox{0.8}{\begin{tabular}{lcccccc} \hline \hline' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '15i| \hline ' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '16i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '17i| & \multicolumn{6}{c}{(a) Hard brakes}  \tabularnewline \cline{2-7} ' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '24i| \hline ' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '25i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '26i| & \multicolumn{6}{c}{(b) Have both hard brakes and turns} \tabularnewline \cline{2-7} ' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '33i| \hline ' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '34i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc '37i| \hline \hline \end{tabular}} ' -cx "$results/tex_files/est_baseline_`name'.tex"

!ex -sc 38,42d -cx "$results/tex_files/est_baseline_`name'.tex"
!ex -sc 1,12d -cx "$results/tex_files/est_baseline_`name'.tex"

restore



** IV 

preserve
keep if _n>=14


gen nm_measures="" 
replace nm_measures="No agg in the trip" if _n==1
replace nm_measures="No agg in the last 5 min" if _n==3
replace nm_measures="Original" if _n==5
replace nm_measures="No agg in the trip" if _n==7
replace nm_measures="No agg in the last 5 min" if _n==9
replace nm_measures="Original" if _n==11
order nm_measures, before(phone_use)

append using $results/main_reg/avg_values.dta


drop if _n>=14 & nm_measures~="full" // use full sample avg
replace nm_measures="Average values" if _n==14
replace nm_measures="Observations" if _n==13

gen temp=_n
replace temp=13 if temp==14
sort temp nm_measures
drop temp

insobs 1, before(1)
replace nm_measures="" if _n==1
replace phone_use="Phone use" if _n==1
replace distance="Distance" if _n==1
replace duration="Duration" if _n==1
replace speed="Speed" if _n==1
replace drive_at_night="Drive at night" if _n==1
replace highway="Highway" if _n==1



texsave using "$results/tex_files/est_iv_`name'.tex", replace  location(htbp) noendash nonames
!ex -sc '13i| \scalebox{0.8}{\begin{tabular}{lcccccc} \hline \hline' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '15i| \hline ' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '16i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '17i| & \multicolumn{6}{c}{(a) Hard brakes}  \tabularnewline \cline{2-7} ' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '24i| \hline ' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '25i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '26i| & \multicolumn{6}{c}{(b) Have both hard brakes and turns} \tabularnewline \cline{2-7} ' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '33i| \hline ' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '34i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc '37i| \hline \hline \end{tabular}} ' -cx "$results/tex_files/est_iv_`name'.tex"

!ex -sc 38,42d -cx "$results/tex_files/est_iv_`name'.tex"
!ex -sc 1,12d -cx "$results/tex_files/est_iv_`name'.tex"

restore

}






******************************************************
* Collect coefficeints for AR(2) model, NM def 1 and 2 
******************************************************


import delimited "$results/main_reg/reg_add_2lags_072020.txt", clear



drop v1 v2

keep if (_n>=4 & _n<=7) | (_n>=16 & _n<=19) | _n==29

forvalues i=3(1)38{

	loc j=`i'-2
	rename v`i' v`j' 
	
} 

forvalues i=1(1)4{
	
	loc j1=(`i'-1)*6+1
	loc j2=(`i'-1)*6+2
	loc j3=(`i'-1)*6+3
	loc j4=(`i'-1)*6+4
	loc j5=(`i'-1)*6+5
	loc j6=`i'*6
	
	preserve 
	
		keep v`j1'-v`j6' 
		drop if v`j1'==""
		
		rename v`j1' phone_use
		rename v`j2' distance
		rename v`j3' duration
		rename v`j4' speed
		rename v`j5' drive_at_night
		rename v`j6' highway
		
		save "$results/coef_temp_`i'.dta", replace
		
	restore
}

use "$results/coef_temp_1.dta", clear
drop if _n==_N
append using "$results/coef_temp_4.dta"

forvalues i=1(1)4{
	erase "$results/coef_temp_`i'.dta"  
}

gen nm_measures="" 
replace nm_measures="$ NM_{it-1} $" if _n==1
replace nm_measures="$ NM_{it-2} $" if _n==3
replace nm_measures="$ NM_{it-1} $" if _n==5
replace nm_measures="$ NM_{it-2} $" if _n==7
order nm_measures, before(phone_use)

append using $results/main_reg/avg_values.dta


drop if _n>=10 & nm_measures~="full"   // use full sample avg
replace nm_measures="Average values" if _n==10
replace nm_measures="Observations" if _n==9



gen temp=_n
replace temp=9 if temp==10
sort temp nm_measures
drop temp

insobs 1, before(1)
replace nm_measures="" if _n==1
replace phone_use="Phone use" if _n==1
replace distance="Distance" if _n==1
replace duration="Duration" if _n==1
replace speed="Speed" if _n==1
replace drive_at_night="Drive at night" if _n==1
replace highway="Highway" if _n==1




texsave using "$results/tex_files/est_iv_add_2lags.tex", replace  location(htbp) noendash nonames
!ex -sc '13i| \scalebox{0.8}{\begin{tabular}{lcccccc} \hline \hline' -cx "$results/tex_files/est_iv_add_2lags.tex"
!ex -sc '15i| \hline ' -cx "$results/tex_files/est_iv_add_2lags.tex"
!ex -sc '16i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_iv_add_2lags.tex"
!ex -sc '17i| & \multicolumn{6}{c}{(a) NM def 1}  \tabularnewline \cline{2-7} ' -cx "$results/tex_files/est_iv_add_2lags.tex"
!ex -sc '22i| \hline ' -cx "$results/tex_files/est_iv_add_2lags.tex"
!ex -sc '23i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_iv_add_2lags.tex"
!ex -sc '24i| & \multicolumn{6}{c}{(b) NM def 2} \tabularnewline \cline{2-7} ' -cx "$results/tex_files/est_iv_add_2lags.tex"
!ex -sc '29i| \hline ' -cx "$results/tex_files/est_iv_add_2lags.tex"
!ex -sc '30i| & & & & & &  \tabularnewline ' -cx "$results/tex_files/est_iv_add_2lags.tex"
!ex -sc '33i| \hline \hline \end{tabular}} ' -cx "$results/tex_files/est_iv_add_2lags.tex"

!ex -sc 34,38d -cx "$results/tex_files/est_iv_add_2lags.tex"
!ex -sc 1,12d -cx "$results/tex_files/est_iv_add_2lags.tex"
